//package com.sinosoft.zyxt;
//
//import java.util.List;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.apache.log4j.Logger;
//
//import com.sinosoft.cnmuseum.servlets.i.IServlet;
//import com.sinosoft.commontools.annotated.rquestMethod;
//import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
//import com.sinosoft.commontools.record.Record;
//import com.sinosoft.zyxt.dao.MbzyUimageDao;
//
//public class MbzyUiamgeServlet implements IServlet{
//	  private static Logger logger = Logger.getLogger(MbzyUiamgeServlet.class);
//	
//	@rquestMethod(MethodNote = ReqMode.All)
//	public Record getAll(HttpServletRequest request, HttpServletResponse response, Record data){
//		Record rel = new Record();
//		
//		MbzyUimageDao a= new MbzyUimageDao();
//		String sql = "select * from MBZY.M_IMAGE";
//		List<Record> bySql = a.getBySql(sql);
//		
//		rel.add("rel", bySql);
//		return rel;
//	}
//
//	@Override
//	public Record getData(HttpServletRequest request, HttpServletResponse response, Record data) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public Record insert(HttpServletRequest request, HttpServletResponse response, Record data) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public Record update(HttpServletRequest request, HttpServletResponse response, Record data) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public Record delete(HttpServletRequest request, HttpServletResponse response, Record data) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//	
//	
//	
//}
package com.sinosoft.zyxt;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.servlets.i.IServlet;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;
import com.sinosoft.zyxt.jdbc.JdbcUtil;
import com.sinosoft.zyxt.task.MbimgTimerTask;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MbzyUiamgeServlet implements IServlet {
	private static Logger logger = Logger.getLogger(MbzyUiamgeServlet.class);

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getAll(HttpServletRequest request, HttpServletResponse response, Record data) {
		Record r = new Record();
		try {
			Integer pageNumber = data.getIntValue("pageNumber");
			Integer pageSize = data.getIntValue("pageSize");
			if (pageNumber == null || pageNumber <= 0) {
				r.add(Status.SATUS, Status.FAILED);
				r.add(Status.MSG, "参数有误");
				return r;
			}
			if (pageSize == null || pageSize <= 0) {
				r.add(Status.SATUS, Status.FAILED);
				r.add(Status.MSG, "参数有误");
				return r;
			}
			Connection connection = JdbcUtil.getConnection();
			PreparedStatement prepareStatement = null;
			ResultSet executeQuery = null;
			Integer startIndex = (pageNumber - 1) * pageSize;
			Integer endIndex = ((pageNumber - 1) * pageSize) + pageSize;
			String sql = "SELECT * FROM (SELECT tt.*, ROWNUM AS rowno FROM (  SELECT m.* FROM M_IMAGE m ORDER BY m.INTERNAL_ID asc) tt WHERE ROWNUM <= ?) table_alias WHERE table_alias.rowno > ?";
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setInt(1, endIndex);
			prepareStatement.setInt(2, startIndex);
			executeQuery = prepareStatement.executeQuery();
			

			List<Record> list = new ArrayList<Record>();
			while (executeQuery.next()) {
				Record m = new Record();
				m.add("name", executeQuery.getString("imgname"));
				m.add("spart_id", executeQuery.getString("spart_id"));
				m.add("id", executeQuery.getString("internal_id"));
				m.add("rawext", executeQuery.getString("rawext"));
				list.add(m);
			}
			r.add("re", list);
			JdbcUtil.close(connection, prepareStatement, executeQuery);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return r;
	}

	@SuppressWarnings("unused")
	@rquestMethod(MethodNote = ReqMode.All)
	public Record download(HttpServletRequest request, HttpServletResponse response, Record data) {
		Record r = new Record();
		try {
			String strValue = data.getStrValue("ID");
			if(strValue==null && strValue=="") {
				r.add(Status.SATUS, Status.FAILED);
				r.add(Status.MSG, "参数有误");
				return r;
			}
			Connection connection = JdbcUtil.getConnection();
			
			String sql = "SELECT M_IMAGE.SUB_PATH as SUB_PATH,M_IMAGE.spart_id as spart_id,M_IMAGE.rawext as rawext,SPART.ABSOLUTE_PATH as ABSOLUTE_PATH FROM M_IMAGE,SPART where M_IMAGE.SPART_ID = SPART.INTERNAL_ID and  M_IMAGE.internal_id = ?";
			PreparedStatement prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, strValue);
			ResultSet executeQuery = prepareStatement.executeQuery();
			String subPath = null;
			String spartId = null;
			String filename = null;
			String source = null;
			while (executeQuery.next()) {
				subPath = executeQuery.getString("SUB_PATH");
				spartId = executeQuery.getString("spart_id");
				filename = executeQuery.getString("rawext");
				source = executeQuery.getString("ABSOLUTE_PATH");
			}
			JdbcUtil.close(connection, prepareStatement, executeQuery);
			
			
			String url = source + subPath + filename;
			String result = url.replaceAll("/", "\\\\");
			
			//开始下载文件
			File file = new File(result);
			String newFileName = filename;
			if (!file.exists()) {
				r.add(Status.SATUS, Status.FAILED);
				r.add(Status.MSG, "文件不存在");
				return r;
			}
			InputStream in = new BufferedInputStream(new FileInputStream(file));
			OutputStream out = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("multipart/form-data");
			response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(newFileName, "UTF-8"));
			response.setContentLength((int) file.length());
			int len = 0;
			byte[] buffer = new byte[1024 * 10];
			while ((len = in.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			out.flush();
			out.close();
			in.close();
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		r.add(Status.SATUS, Status.OK);
		return r;
	}

	
	
	@Override
	public Record getData(HttpServletRequest request, HttpServletResponse response, Record data) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Record insert(HttpServletRequest request, HttpServletResponse response, Record data) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Record update(HttpServletRequest request, HttpServletResponse response, Record data) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Record delete(HttpServletRequest request, HttpServletResponse response, Record data) {
		// TODO Auto-generated method stub
		return null;
	}

}
